package com.muedsa.tvbox.bilibili.service

import com.muedsa.tvbox.bilibili.BilibiliConst
import com.muedsa.tvbox.bilibili.TestCookieSaver
import com.muedsa.tvbox.bilibili.TestOkHttpClient
import com.muedsa.tvbox.bilibili.TimberUnitTestTree
import com.muedsa.tvbox.bilibili.helper.BiliCookieHelper
import com.muedsa.tvbox.bilibili.helper.LiveChatPacketUtil
import com.muedsa.tvbox.tool.ChromeUserAgent
import com.muedsa.tvbox.tool.createJsonRetrofit
import com.muedsa.tvbox.tool.toRequestBuild
import kotlinx.coroutines.runBlocking
import org.junit.BeforeClass
import org.junit.Test
import timber.log.Timber
import java.nio.ByteBuffer

class LiveDanmakuDataFlowTest {

    private val liveApiService by lazy {
        createJsonRetrofit(
            baseUrl = "${BilibiliConst.LIVE_API_URL}/",
            service = BilibiliLiveApiService::class.java,
            okHttpClient = TestOkHttpClient
        )
    }

    @Test
    fun test() {
        val roomId = 7734200L
        runBlocking {
            val resp = liveApiService.getDanmuInfo(id = roomId)
            check(resp.data != null)
            val hostInfo = resp.data.hostList.first()
            Timber.i("LiveDanmakuDataFlow start")
            val flow = LiveDanmakuDataFlow(
                uid = BiliCookieHelper.getCookeValue(
                    cookieSaver = TestCookieSaver,
                    cookieName = BiliCookieHelper.COOKIE_UID,
                    defaultValue = "0",
                )!!.toLong(),
                roomId = roomId,
                b3 = BiliCookieHelper.getCookeValue(
                    cookieSaver = TestCookieSaver,
                    cookieName = BiliCookieHelper.COOKIE_B_3
                )!!,
                token = resp.data.token,
                request = "wss://${hostInfo.host}:${hostInfo.wssPort}/sub"
                    .toRequestBuild()
                    .header("User-Agent", ChromeUserAgent)
                    .build(),
                okHttpClient = TestOkHttpClient,
                debug = true,
            )
            flow.flow.collect { value ->
                println(value.content)
            }
        }
    }

    @Test
    fun decompressBrotliBytes_test() {
        val payload = byteArrayOf(
            27,
            -59,
            3,
            0,
            -84,
            10,
            108,
            99,
            81,
            -24,
            95,
            -91,
            -109,
            -91,
            93,
            10,
            67,
            79,
            121,
            -4,
            104,
            69,
            -51,
            114,
            -9,
            91,
            -53,
            3,
            -114,
            -113,
            36,
            73,
            -92,
            -41,
            -88,
            29,
            69,
            -119,
            6,
            20,
            -73,
            -88,
            121,
            -124,
            -83,
            52,
            -102,
            83,
            -7,
            -24,
            -62,
            3,
            42,
            61,
            -72,
            50,
            110,
            28,
            58,
            33,
            -46,
            51,
            67,
            82,
            101,
            58,
            -88,
            -120,
            -22,
            -117,
            79,
            63,
            -73,
            -59,
            -123,
            26,
            119,
            -17,
            -51,
            12,
            17,
            -115,
            -65,
            -31,
            18,
            -54,
            -30,
            -114,
            68,
            -55,
            -112,
            -84,
            -47,
            80,
            -87,
            77,
            96,
            98,
            -65,
            -6,
            89,
            58,
            51,
            81,
            -60,
            28,
            113,
            -78,
            -73,
            -19,
            -23,
            117,
            77,
            99,
            1,
            -124,
            -104,
            97,
            -1,
            -121,
            -72,
            73,
            -64,
            -27,
            22,
            -66,
            120,
            88,
            2,
            50,
            -10,
            -33,
            61,
            -40,
            31,
            -22,
            8,
            61,
            -60,
            33,
            25,
            -12,
            -77,
            77,
            -35,
            44,
            0,
            30,
            -88,
            11,
            56,
            -70,
            22,
            -73,
            -110,
            26,
            68,
            19,
            58,
            -92,
            116,
            4,
            -122,
            0,
            41,
            -125,
            -95,
            -99,
            -96,
            -79,
            44,
            -46,
            1,
            -88,
            115,
            90,
            -77,
            70,
            -84,
            -65,
            124,
            98,
            -92,
            45,
            119,
            92,
            -8,
            109,
            -101,
            -39,
            41,
            -113,
            36,
            -114,
            13,
            103,
            117,
            84,
            89,
            78,
            37,
            -17,
            117,
            -95,
            50,
            95,
            122,
            -59,
            -53,
            82,
            61,
            -116,
            23,
            105,
            12,
            -23,
            72,
            -37,
            -95,
            61,
            -96,
            29,
            -22,
            102,
            -97,
            90,
            -32,
            66,
            -127,
            97,
            92,
            22,
            49,
            12,
            123,
            5,
            16,
            -8,
            -46,
            22,
            -112,
            66,
            90,
            103,
            -76,
            12,
            -38,
            22,
            114,
            -69,
            123,
            102,
            5,
            87,
            -36,
            56,
            35,
            -126,
            -86,
            -39,
            -108,
            105,
            -116,
            -91,
            11,
            1,
            -108,
            -11,
            101,
            -71,
            108,
            -67,
            92,
            17,
            35,
            -37,
            87,
            -126,
            103,
            2,
            -96,
            -40,
            100,
            -78,
            -84,
            125,
            -84,
            -83,
            18,
            -34,
            24,
            -57,
            88,
            -64,
            26,
            -122,
            90,
            -17,
            -24,
            -27,
            -78,
            -48,
            -28,
            23,
            -40,
            2,
            13,
            -28,
            106,
            103,
            -21,
            56,
            -33,
            -100,
            104,
            69,
            -44,
            20,
            -5,
            -112,
            33,
            -66,
            -103,
            85,
            123,
            108,
            -93,
            -100,
            -6,
            -34,
            91,
            -58,
            54,
            127,
            87,
            -91,
            -55,
            48,
            50,
            -121,
            9,
            -95,
            -115,
            47,
            -75,
            48,
            50,
            91,
            42,
            109,
            -118,
            51,
            109,
            -20,
            57,
            -41,
            -26,
            -47,
            1,
            -116,
            127,
            0,
            110,
            95,
            111,
            -73,
            63,
            71,
            105,
            -46,
            -83,
            -121,
            -50,
            33,
            -112,
            71,
            -64,
            68,
            109,
            -38,
            -113,
            -83,
            -3,
            122,
            -66,
            -78,
            -65,
            116,
            68,
            -88,
            -71,
            -72,
            42,
            -58,
            -39,
            75,
            -116,
            -14,
            99,
            51,
            -82,
            -4,
            -127,
            -33,
            -74,
            -120,
            -42,
            106,
            -97,
            -78,
            113,
            84,
            4,
            103,
            -70,
            84,
            -63,
            12,
            93,
            74,
            89,
            61,
            -26,
            42,
            2,
            -103,
            110,
            -70,
            -64,
            102,
            -10,
            57,
            116,
            94,
            -95,
            -117,
            -41,
            117,
            -75,
            -10,
            -71,
            59,
            -40,
            -92,
            116,
            28,
            -40,
            56,
            -11,
            -27,
            -69,
            -28,
            -126,
            -80,
            -1,
            -35,
            85,
            -20,
            -101,
            99,
            110,
            -112,
            72,
            -10,
            31
        )
        val bytes = LiveChatPacketUtil.decompressBrotliBytes(payload)
        val chatPacket = LiveChatPacketUtil.decode(ByteBuffer.wrap(bytes))
        println(chatPacket)
        val cmdList = LiveChatPacketUtil.decodeCMD(chatPacket.payload)
        cmdList.forEach { println(it) }
    }

    companion object {
        @BeforeClass
        @JvmStatic
        fun before() {
            Timber.plant(TimberUnitTestTree())
        }
    }
}